<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[1].id}`">{{goodDetail.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodDetail.categories[0].id}`">{{goodDetail.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goodDetail.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 图片预览区 -->
          <GoodsImage :imageList="goodDetail.mainPictures"/>
          <!-- 销量组件 -->
          <GoodsSales :goodDetail="goodDetail"/>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodName :goodDetail="goodDetail"/>
          <GoodsSku :goods="goodDetail" @change="getSku"/>
          <!-- vue2双向数据绑定  父组件 :value @input-->
          <!-- vue3双向数据绑定 父组件 :modelValue="buyCount" @update:modelValue="" -->
          <XtxNumbox :max="goodDetail.inventory" v-model="buyCount"/>
          <XtxButton type="primary" style="margin-top:20px">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"/>
          <GoodsHot :type="2"/>
          <GoodsHot :type="3"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import { findGoods } from '@/api/goods'
import { onMounted, ref, provide } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodName,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    // 商品购买数量
    const buyCount = ref(1)
    // 商品详情数据
    const goodDetail = ref({})
    // 路由实例
    const route = useRoute()
    // 向下注入商品数据
    provide('goods', goodDetail)

    // 获取商品x详情数据方法
    const getGoodDetail = async (id) => {
      const res = await findGoods(id)
      console.log(res)
      goodDetail.value = res.result
    }

    // 获取sku属性
    const getSku = (sel) => {
      console.log(sel)
      if (sel.id) {
        goodDetail.value.price = sel.price
        goodDetail.value.oldPrice = sel.oldPrice
        goodDetail.value.inventory = sel.inventory
      }
    }

    // 调用
    onMounted(() => {
      getGoodDetail(route.params.id)
    })

    return { goodDetail, getSku, buyCount }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
       width: 580px;
       height: 600px;
       padding: 30px 50px;
     }
     .spec {
       flex: 1;
       padding: 30px 30px 30px 0;
     }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
